import pymysql
import matplotlib

matplotlib.use('Agg')
import matplotlib.pyplot as plt
from pylab import mpl

mpl.rcParams['font.sans-serif'] = ['SimHei']

if __name__ == '__main__':
    # **********begin********** #

    # 获取连接对象
    conn = pymysql.connect(
        host='localhost', port=3306,
        user='root', passwd='123123',
        charset='utf8', db="nudt"
    )

    # 获取光标
    cursor = conn.cursor()

    # 查询男老师数量并将数值赋予给变量 maleNum
    cursor.execute('select count(*) from Teacher where Tsex="男"')
    maleNum = cursor.fetchone()[0]
    # 查询女老师数量将数值赋予给变量 femaleNum
    cursor.execute('select count(*) from Teacher where Tsex="女"')
    femaleNum = cursor.fetchone()[0]

    # 将变量 maleNum 与变量 femaleNum 输出到控制台，格式如 -->  变量名:数值
    # 例如： maleNum:100
    # 注意：一行输出一个变量,请先输出变量 maleNum ，其次是变量 femaleNum

    print(f"maleNum: {maleNum}")
    print(f"femaleNum: {femaleNum}")
    # 绘制柱状图
    plt.xticks(fontsize=12)
    plt.yticks([])  # 不显示y轴刻度
    plt.text(0, maleNum * 0.5, maleNum, ha='center', fontsize=30)
    plt.text(1, femaleNum * 0.5, femaleNum, ha='center', fontsize=30)
    plt.bar(range(2), (maleNum, femaleNum), color='gy', tick_label=('男', '女'))
    plt.savefig("img/1.png")

    # 关闭资源

    # **********end********** #
